import store from "@/store";

export const mixin = {
  data() {
    return {
      timer: null
    };
  },
  methods: {
    // 全局定时器方法
    timerFn(fn, millisecond, loading) {
      const axiosCancels = store.state.axiosCancels;
      if (axiosCancels.length) {
        axiosCancels.forEach(cancel => cancel && cancel());
        store.commit("SET_AXIOS_CANCELS", []);
      }
      this[fn]().then(() => {
        this[loading] = false;
      }).finally(() => {
        this.timer = setTimeout(() => {
          this.timerFn(fn, millisecond, loading);
        }, millisecond);
      });
    }
  },
  beforeDestroy() {
    clearTimeout(this.timer);
    this.timer = null;
  },
  mounted() {
  }
};